package com.rc.web.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.rc.common.constant.Code;
import com.rc.common.exception.BusinessException;
import com.rc.pojo.dto.ill.IllAddDTO;
import com.rc.pojo.dto.ill.IllPageQuery;
import com.rc.pojo.dto.ill.IllUpdateDTO;
import com.rc.pojo.po.Ill;
import com.rc.pojo.po.Plant;
import com.rc.pojo.vo.ill.*;
import com.rc.pojo.vo.knowldege.KnowledgeVO;
import com.rc.pojo.vo.knowledgevideo.KnowledgeVideoVO;
import com.rc.web.converter.IllConverter;
import com.rc.web.mapper.IllMapper;
import com.rc.web.service.IllService;
import com.rc.web.service.KnowledgeService;
import com.rc.web.service.KnowledgeVideoService;
import com.rc.web.service.PlantService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 病虫害服务实现类
 * 提供病虫害相关的业务逻辑实现
 */
@Service
@RequiredArgsConstructor
public class IllServiceImpl extends ServiceImpl<IllMapper, Ill> implements IllService {
    // 病虫害转换器
    private final IllConverter illConverter;
    // 植物服务
    private final PlantService plantService;
    // 知识服务
    private final KnowledgeService knowledgeService;
    // 知识视频服务
    private final KnowledgeVideoService videoService;

    /**
     * 添加病虫害
     * @param dto 病虫害添加DTO
     */
    @Override
    public void add(IllAddDTO dto) {
        // 检查植物是否存在
        var plant = plantService.getById(dto.getPlantId());
        if (plant == null){
            throw new BusinessException(Code.NO_SUCH_PLANT);
        }

        // 转换DTO为PO并插入数据库
        Ill ill = illConverter.addDtoToPo(dto);
        mapper.insertSelective(ill);
    }

    /**
     * 获取常见病虫害信息
     * @param pantId 植物ID
     * @return 常见病虫害VO
     */
    @Override
    public IllCommonVO getCommonIll(Long pantId) {
        // 获取所有病虫害
        var illList =  mapper.selectByPlantId(pantId);
        var illListVOList = illList.stream().map(illConverter::toListVO).toList();

        // 获取所有相关视频
        var illIdList = illList.stream().map(Ill::getId).toList();
        var videoList = videoService.getListByIllIds(illIdList);

        return IllCommonVO.builder()
                .illList(illListVOList)
                .videoList(videoList)
                .build();
    }

    /**
     * 获取病虫害详情
     * @param id 病虫害ID
     * @return 病虫害详情VO
     */
    @Override
    public IllDetailVO getDetail(Long id) {
        var ill =  mapper.selectOneById(id);
        var detailVO = illConverter.toDetailVO(ill);

        // 获取相关知识点
        var illIds = List.of(ill.getId());
        var knowledgeList = knowledgeService.getListByIllIds(illIds);
        detailVO.setKnowledge(knowledgeList);

        return detailVO;
    }

    /**
     * 分页查询病虫害
     * @param dto 分页查询条件
     * @return 分页结果
     */
    @Override
    public Page<IllPageVO> getPage(IllPageQuery dto) {
        return mapper.selectPage(dto);
    }

    /**
     * 根据ID获取病虫害信息
     * @param id 病虫害ID
     * @return 病虫害VO
     */
    @Override
    public IllVO getOneById(Long id) {
        var ill = mapper.selectOneById(id);
        return illConverter.toVO(ill);
    }

    /**
     * 更新病虫害信息
     * @param dto 更新DTO
     */
    @Override
    public void updateOneById(IllUpdateDTO dto) {
        var ill = illConverter.updateDTO2Po(dto);
        var success = this.updateById(ill);
        if (!success){
            throw new BusinessException(Code.NO_SUCH_PLANT);
        }
    }

    /**
     * 根据ID删除病虫害
     * @param id 病虫害ID
     */
    @Override
    public void deleteOneById(Long id) {
        var success = this.removeById(id);
        if (!success){
            throw new BusinessException(Code.NO_SUCH_PLANT);
        }
    }

    /**
     * 获取所有病虫害列表
     * @return 病虫害VO列表
     */
    @Override
    public List<IllVO> getList() {
        var list = mapper.selectAll();
        return list.stream()
                .map(illConverter::toVO)
                .toList();
    }
}
